import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;


public class ParticleDialog extends JDialog implements ActionListener
{
	public JTextField radiusTF, xPositionTF, yPositionTF, xVelocityTF, yVelocityTF, massTF;
	JButton submitButton, doneButton;
	World parent;
	public ParticleDialog(World parent, String title)
	{
		super(parent, title);
		this.parent = parent;
		setLayout(new GridLayout(7, 2));
		add(new JLabel("Radius"), "1");
		radiusTF = new JTextField();
		add(radiusTF, "1");
		add(new JLabel("X Position"), "2");
		xPositionTF = new JTextField();
		add(xPositionTF, "2");
		add(new JLabel("Y Position"), "3");
		yPositionTF = new JTextField();
		add(yPositionTF, "3");
		add(new JLabel("X Velocity"), "4");
		xVelocityTF = new JTextField();
		add(xVelocityTF, "4");
		add(new JLabel("Y Velocity"), "5");
		yVelocityTF = new JTextField();
		add(yVelocityTF, "5");
		add(new JLabel("Mass"));
		massTF = new JTextField();
		add(massTF, "6");
		
		submitButton = new JButton("Add");
		submitButton.addActionListener(this);
		add(submitButton, "7");
		doneButton = new JButton("Done");
		add(doneButton, "7");
		
		doneButton.addActionListener(this);
		setLocationRelativeTo(parent);
		pack();
		
	}
	public void setParticlePosition(double x, double y)
	{
		xPositionTF.setText(String.valueOf(x));
		yPositionTF.setText(String.valueOf(y));
	}
	public void clear()
	{
		radiusTF.setText(null);
		xPositionTF.setText(null);
		yPositionTF.setText(null);
		xVelocityTF.setText(null);
		yVelocityTF.setText(null);
	}
	public void actionPerformed(ActionEvent ae)
	{
		if(ae.getSource() == submitButton)
		{
			try{
				parent.addObject(Double.parseDouble(radiusTF.getText()), Double.parseDouble(xPositionTF.getText()), Double.parseDouble(yPositionTF.getText()), Double.parseDouble(xVelocityTF.getText()), Double.parseDouble(yVelocityTF.getText()), Double.parseDouble(massTF.getText()));
			}
			catch(Exception e)
			{
				JOptionPane.showMessageDialog(parent, "Invalid parameters.");
				clear();
			}
		}
		if(ae.getSource() == doneButton)
			setVisible(false);
	}
}
